import os
from openpyxl import load_workbook, Workbook
from tqdm import tqdm

class AddBudId:
    def __init__(self):
        self.dirs = os.listdir('datas/')
        self.all_datas = []

    def read_datas(self):
        print("正在合并配置文件...")
        for dir in tqdm(self.dirs):
            wb = load_workbook('datas/' + dir)
            try:
                sheet = wb['Bug']
            except:
                sheet = wb['Sheet']
            max_line = sheet.max_row
            for i in tqdm(range(max_line - 1)):
                tmp_datas = []
                tmp_datas.append(sheet.cell(i + 2, 1).value)
                tmp_datas.append(sheet.cell(i + 2, 3).value)
                tmp_datas.append(sheet.cell(i + 2, 4).value)
                tmp_datas.append(sheet.cell(i + 2, 10).value)
                tmp_datas.append(sheet.cell(i + 2, 8).value)
                if tmp_datas in self.all_datas:
                    continue
                self.all_datas.append(tmp_datas)

    def write_excel(self):
        sava_path = "tmp_datas/禅道ID号配置文件.xlsx"
        print("正在写入数据...")
        wb = Workbook()
        ws = wb.active
        title = ['bug号', '分支', '所属模块', '严重程度', 'bug标题']
        self.all_datas.insert(0, title)
        for index, value in enumerate(self.all_datas):
            for index2, value2 in enumerate(value):
                ws.cell(index + 1, index2 + 1, value=value2)
        wb.save(sava_path)
        wb.close()
        print("写入成功，数据保存在：", sava_path,"配置文件共计{0}条".format(len(self.all_datas)))

    def main(self):
        self.read_datas()
        self.write_excel()


if __name__ == '__main__':
    ids = AddBudId()
    ids.main()
